package com.vogue.supplymall.shop.controller;

import com.vogue.common.http.HttpUtils;
import com.vogue.common.http.InvalidAccessException;
import com.vogue.common.http.Result;
import com.vogue.supplymall.common.domain.dto.HomePageInfo;
import com.vogue.supplymall.common.domain.dto.LocationInfo;
import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.match.domain.service.MatchOrganService;
import com.vogue.supplymall.shop.common.CheckSearchInfoUtils;
import com.vogue.supplymall.shop.domain.service.HomePageBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

import javax.annotation.Resource;

/**
 * The type Home page controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2017-12-21
 */
@RestController
public class HomePageController extends BaseController {
    
    public static final Logger LOGGER_HOMEPAGECONTROLLER = LoggerFactory.getLogger(HomePageController.class);

    @Resource
    private HomePageBiz homePageBiz;
    @Resource
    private MatchOrganService matchOrganService;

    /**
     * Hone page info response entity.
     * 首页信息-APP端
     * @param appver the appver
     * @param searchInfo the search info
     * @return the response entity
     */
    @RequestMapping(value = "/ns/{appver}/homepage", method = RequestMethod.POST)
    public ResponseEntity<Object> homePageInfo(@PathVariable("appver") String appver, @RequestBody SearchInfo searchInfo) {
        LOGGER_HOMEPAGECONTROLLER.info("homePageInfo:start in " + System.currentTimeMillis());
        HomePageInfo homePageInfo = new HomePageInfo();
        if (CheckSearchInfoUtils.apply(searchInfo)) {
            LOGGER_HOMEPAGECONTROLLER.info("searchInfo check failed!");
            return new ResponseEntity(homePageInfo.getShop(), HttpStatus.OK);
        }
        homePageInfo.setShop(homePageBiz.getShop(searchInfo));
        LOGGER_HOMEPAGECONTROLLER.info("sessionid:" + searchInfo.getSessionId() + ", return result.shop in ," + System.currentTimeMillis());
        return new ResponseEntity<>(homePageInfo.getShop(), HttpStatus.OK);
    }

    /**
     *  首页信息-PC端
     * @param appver appver
     * @param searchInfo searchInfo
     * @return the response entity
     */
    @RequestMapping(value = "/ns/{appver}/shop/homepage4PC", method = RequestMethod.POST)
    public ResponseEntity<Object> homePageInfo4PC(@PathVariable("appver") String appver, @RequestBody SearchInfo searchInfo) {
        LOGGER_HOMEPAGECONTROLLER.info("homePageInfo:start in " + System.currentTimeMillis());
        HomePageInfo homePageInfo = new HomePageInfo();
        if (CheckSearchInfoUtils.apply(searchInfo)) {
            LOGGER_HOMEPAGECONTROLLER.info("searchInfo check failed!");
            return new ResponseEntity(homePageInfo.getShop(), HttpStatus.OK);
        }
        homePageInfo.setOrgan(matchOrganService.findOrgan(searchInfo));
        homePageInfo.setPrivatePlatformInfo(homePageBiz.getPrivatePlatformInfo(searchInfo));

        LOGGER_HOMEPAGECONTROLLER.info("sessionid:" + searchInfo.getSessionId() + ", return result.shop in ," + System.currentTimeMillis());
        return new ResponseEntity<>(homePageInfo, HttpStatus.OK);
    }

    /**
     * 店铺位置
     * @param appver appver
     * @param locationInfo locationInfo
     * @return ResponseEntity
     */
    @RequestMapping(value = "/ns/{appver}/shop/location", method = RequestMethod.POST)
    public ResponseEntity<Object> locationInfo(@PathVariable("appver") String appver, @RequestBody LocationInfo locationInfo) {

        Result result = null;
        try {
            result = HttpUtils.get("http://api.map.baidu.com/geocoder/v2/?location="
                    + locationInfo.getLatitude() + "," + locationInfo.getLongitude()
                    + "&output=json&pois=1&ak=" + locationInfo.getKey(), null, null, "UTF-8");
        } catch (IOException | InvalidAccessException e) {
            // TODO Auto-generated catch block
            LOGGER_HOMEPAGECONTROLLER.error("LocationInfo获取地理位置失败:" + e.toString());
            throw new DataNotFindException("LocationInfo");
        }
        return new ResponseEntity<>(result.getResult(), HttpStatus.OK);
    }
}
